#ifndef __CHANNNEL_RANK_MGR_H__
#define __CHANNNEL_RANK_MGR_H__
#include <iostream>
#include <vector>
#include <map>
#include <time.h>
#include <string>
#include "dbmgr.h"
#include "proto_20000_rank.h"
#include "common_def.h"
#include "timefuncdef.h"
#include "config_mgr.h"
#include "proto_svr.h"
#include <sys/time.h>
#include <algorithm>
#include "rank_mgr.h"
using namespace std;

typedef map<int, vector<RankData> > mapChannel2RankData;       //每个渠道下的排行信息
typedef map<int, mapChannel2RankData> mapRankType2RankMapData;  //每个排行榜类型下渠道排行榜信息

const int gChannelTypeList[] = {RANK_TYPE_JB, RANK_TYPE_DT, RANK_TYPE_MATERIAL}; //支持渠道的排行榜类型
const int gChannelTypeLength = sizeof(gChannelTypeList)/sizeof(gChannelTypeList[0]); //长度
const int gSaveSizeLimit = 5; //数据库每条保存的用户数据限制
const int gChannelIDLimit = 1000; //渠道号限制
class GlobalServer;

static bool coinRankSortFunc(const RankData &data1, const RankData &data2);
static bool warheadRankSortFunc(const RankData &data1, const RankData &data2);
static bool meterialRankSortFunc(const RankData &data1, const RankData &data2);

class ChannelRankMgr
{
public:
	ChannelRankMgr(){};
	~ChannelRankMgr(){};
	void init(GlobalServer *pServer);
	void load(const int &nRankType=0); //只保留实时榜
	void save(const int &nRankType=0, const int &nChannel=0);
	bool addRankData(RankData &data);
	bool addRankData(const int &nRankType, const int &nChannelID, RankData &data);
	void sort(const int &nRankType=0, const int &nChannel=0, const bool &bSave=true);
	void initRandData(const int &nType, const int &nChannel, const string &strBuffer);
	bool getRankList(const int &nUid, proto20rank::CProto20000Rank &data);
	void initPlayerRandData(RankData &data);
	void initPlayerRandData(const string &strTool, map<int, long> &mapOut);
	void getRankSaveString(vector<RankData> &data, vector<string> &vecOut);
	string getPlayerChannel(const int &nUid);
	void rankDataOverTimeCheck(const int &nRankType, const int &nChannelID);
	int getPlayerChannelID(const int &nUid, string &strChannel);
	void fresh(time_t now);
	bool rankDataCanInsert(const int &nRankType, map<int, long> &mapMin, map<int, long> &mapCur, bool &bSort);

protected:
	mapRankType2RankMapData m_mapRealTimeRankData; //实时榜
	mapRankType2RankMapData m_mapShowTimeRankData; //展示榜
	map<int, map<int, int> > m_mapSaveIndex;       //用来记录排行榜的记录索引
	int m_nRefreshTime;                            //展示榜刷新时间
private:
	DBMgr *m_pDbMgr;
	base::Logger *m_pRollLog;
};


#endif